package com.sscaipiao.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class ForgetPasswordActivity extends Activity implements OnClickListener{
	
	private Button backButton;
	private Button validateButton;
	private Button submitButton;
	
	private EditText  txt_phone;
	private EditText forget_password_txt_validate_code;
	private String validateCode;
	
	private User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forget_password);
		
		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
			}
		}
		
		backButton =  (Button) findViewById(R.id.forget_password_button_back);
		submitButton = (Button) findViewById(R.id.forget_password_submit);
		validateButton = (Button) findViewById(R.id.forget_password_button_validate_code);
		
		backButton.setOnClickListener(this);
		submitButton.setOnClickListener(this);
		validateButton.setOnClickListener(this);
		
		txt_phone = (EditText)  findViewById(R.id.forget_password_phone);
		forget_password_txt_validate_code = (EditText)  findViewById(R.id.forget_password_txt_validate_code);
	}


	@Override
	public void onClick(View v) {
		if(R.id.forget_password_button_back == v.getId()) {

//			Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
//			Bundle b = new Bundle();
//			intent.putExtras(b);
//			startActivity(intent);
			
			ForgetPasswordActivity.this.finish();
			
		} else if (R.id.forget_password_submit == v.getId()){
			if(StringUtil.isEmptyString(txt_phone.getText().toString())){
				Toast.makeText(ForgetPasswordActivity.this, "请填写手机号",
						Toast.LENGTH_SHORT).show();
			} else 	if(StringUtil.isEmptyString(forget_password_txt_validate_code.getText().toString())){
				Toast.makeText(ForgetPasswordActivity.this, "请填写验证码",
						Toast.LENGTH_SHORT).show();
			} else if( ! validateCode.equals(forget_password_txt_validate_code.getText().toString())){
				Toast.makeText(ForgetPasswordActivity.this, "验证码不正确,请重新填写或获取",
						Toast.LENGTH_SHORT).show();
			} else {
				String retStr = UserDataServiceHelper.getForgetPassword(ForgetPasswordActivity.this, txt_phone.getText().toString(), forget_password_txt_validate_code.getText().toString());
				if(Constants.LOGIN_SUCCESS_CODE.equals(retStr.split(Constants.STRING_SPLITE)[0])){
					Toast.makeText(ForgetPasswordActivity.this, "密码已发送,请返回登录后修改",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(ForgetPasswordActivity.this, retStr.split(Constants.STRING_SPLITE)[1],
							Toast.LENGTH_SHORT).show();
				}

			}
			

		}else if (R.id.forget_password_button_validate_code == v.getId()) {
			//EditText phone =  (EditText) findViewById(R.id.forget_password_phone);
			if (StringUtil.isNotEmptyString(txt_phone.getText().toString())){
				Toast.makeText(ForgetPasswordActivity.this, "验证码已发送，请查收",
						Toast.LENGTH_SHORT).show();
				
				 String retStr = UserDataServiceHelper.getValidateCode(ForgetPasswordActivity.this,
						 txt_phone.getText().toString(),user);
				 String[] result  = retStr.split(Constants.STRING_SPLITE);
				 if(result.length > 0) {
					 if(Constants.LOGIN_SUCCESS_CODE.equals(result[0])) {
						 validateCode = result[2];
					 } else {
							new AlertDialog.Builder(this).setTitle("错误")  
			                .setMessage(result[1])  
			                .setPositiveButton("确定", null)  
			                .show(); 
					 }
				 } else {
						new AlertDialog.Builder(this).setTitle("错误")  
		                .setMessage("请检查网络连接情况")  
		                .setPositiveButton("确定", null)  
		                .show();  
				 }
			} else {
				new AlertDialog.Builder(this).setTitle("错误")  
                .setMessage("请先填写手机号码")  
                .setPositiveButton("确定", null)  
                .show();  
			}
			
		} 
		

	}
}
